﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TheSnake
{
    public static class CollisionDispatcher
    {
        public static void HandleCollisions(List<MovingObject> movingObjects, List<StaticObject> staticObjects)
        {
            HandleMovingWithStaticCollisions(movingObjects, staticObjects);
        }

        public static void HandleCollisions(List<MovingObject> movingObjects1, List<MovingObject> movingObjects2)
        {
            HandleMovingCollisions(movingObjects1, movingObjects2);
        }

        public static void HandleCollisions(List<StaticObject> staticObjects1, List<StaticObject> staticObjects2)
        {
            HandleStaticCollisions(staticObjects1, staticObjects2);
        }

        private static void HandleStaticCollisions(List<StaticObject> staticObjects1, List<StaticObject> staticObjects2)
        {
            foreach (var staticObject1 in staticObjects1)
            {
                foreach (var staticObject2 in staticObjects2)
                {
                    if (staticObject1 != staticObject2)
                    {
                        if (staticObject1.TopLeft.Row == staticObject2.TopLeft.Row &&
                            staticObject1.TopLeft.Col == staticObject2.TopLeft.Col)
                        {
                            GameObject obj = null;
                            if (staticObject1 is Bonus)
                            {
                                obj = staticObject1;
                            }
                            else if (staticObject2 is Bonus)
                            {
                                obj = staticObject2;
                            }
                            throw new CollisionException("Static collision exception!", obj);
                        }
                    }
                }
            }
        }

        private static void HandleMovingCollisions(List<MovingObject> movingObjects1, List<MovingObject> movingObjects2)
        {
            foreach (var movingObject1 in movingObjects1)
            {
                List<string> hitByMovingCollisionGroups = new List<string>();

                foreach (var movingObject2 in movingObjects2)
                {
                    if (movingObject1 != movingObject2)
                    {
                        if (movingObject1.TopLeft.Row == movingObject2.TopLeft.Row &&
                            movingObject1.TopLeft.Col == movingObject2.TopLeft.Col)
                        {
                            movingObject1.RespondToCollision(
                                new CollisionData(movingObject2.GetCollisionGroupString())
                            );

                            movingObject2.RespondToCollision(
                                new CollisionData(movingObject1.GetCollisionGroupString())
                            );
                        }
                    }
                }
            }
        }

        private static void HandleMovingWithStaticCollisions(List<MovingObject> movingObjects, List<StaticObject> staticObjects)
        {
            foreach (var movingObject in movingObjects)
            {
                List<string> hitByMovingCollisionGroups = new List<string>();

                foreach (var staticObject in staticObjects)
                {
                    if (movingObject.TopLeft.Row == staticObject.TopLeft.Row &&
                        movingObject.TopLeft.Col == staticObject.TopLeft.Col)
                    {
                        if (movingObject.GetCollisionGroupString() == "food")
                        {
                            throw new CollisionException("Food collision exception!", movingObject);
                        }

                        movingObject.RespondToCollision(
                        new CollisionData(staticObject.GetCollisionGroupString())
                            );
                        staticObject.RespondToCollision(
                        new CollisionData(movingObject.GetCollisionGroupString())
                            );
                    }
                }
            }
        }
    }
}
